package com.dengyuanke.upms.logic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengyuanke.common.core.result.R;
import com.dengyuanke.common.log.annotation.SysLog;
import com.dengyuanke.upms.domain.entity.Dict;
import com.dengyuanke.upms.domain.entity.DictItem;
import com.dengyuanke.upms.logic.service.IDictItemService;
import com.dengyuanke.upms.logic.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private IDictItemService dictItemService;

    @Autowired
    private IDictService dictService;

    /**
     * 通过id查询数据字典
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.success(dictService.getById(id));
    }

    /**
     * 分页查询字典信息
     *
     * @param page
     * @param dict
     * @return
     */
    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, Dict dict) {
        return R.success(dictService.page(page, Wrappers.query(dict)));
    }

    /**
     * 通过字典类型查找字典
     *
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/type/{type}")
    public R getDictByType(@PathVariable String type) {
        return R.success(dictItemService.list(Wrappers
                .<DictItem>query().lambda()
                .eq(DictItem::getType, type)));
    }

    /**
     * 添加字典
     *
     * @param dict 字典信息
     * @return success、false
     */
    @SysLog("添加字典")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_dict_add')")
    public R save(@Valid @RequestBody Dict dict) {
        return R.success(dictService.save(dict));
    }

    /**
     * 删除字典
     *
     * @param id ID
     * @return R
     */
    @SysLog("删除字典")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_dict_del')")
    public R removeById(@PathVariable Long id) {
        return R.success(dictService.removeDict(id));
    }

    /**
     * 修改字典
     *
     * @param dict 字典信息
     * @return success/false
     */
    @PutMapping
    @SysLog("修改字典")
    @PreAuthorize("@pms.hasPermission('sys_dict_edit')")
    public R updateById(@Valid @RequestBody Dict dict) {
        return R.success(dictService.updateById(dict));
    }

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param dictItem 字典项
     * @return
     */
    @GetMapping("/item/page")
    public R getDictItemPage(Page page, DictItem dictItem) {
        return R.success(dictItemService.page(page, Wrappers.query(dictItem)));
    }

    /**
     * 通过id查询字典项
     *
     * @param id id
     * @return R
     */
    @GetMapping("/item/{id}")
    public R getDictItemById(@PathVariable("id") Integer id) {
        return R.success(dictItemService.getById(id));
    }

    /**
     * 新增字典项
     *
     * @param dictItem 字典项
     * @return R
     */
    @SysLog("新增字典项")
    @PostMapping("/item")
    public R save(@RequestBody DictItem dictItem) {
        return R.success(dictItemService.save(dictItem));
    }

    /**
     * 修改字典项
     *
     * @param dictItem 字典项
     * @return R
     */
    @SysLog("修改字典项")
    @PutMapping("/item")
    public R updateById(@RequestBody DictItem dictItem) {
        return R.success(dictItemService.updateById(dictItem));
    }

    /**
     * 通过id删除字典项
     *
     * @param id id
     * @return R
     */
    @SysLog("删除字典项")
    @DeleteMapping("/item/{id}")
    public R removeDictItemById(@PathVariable Integer id) {
        return R.success(dictItemService.removeById(id));
    }


}

